from fastapi.encoders import jsonable_encoder
from sqlalchemy import Boolean, Column, Integer, String
from sqlalchemy.orm import relationship
from app.models.base import Base, TimestampMixin
from app.utils.str_utils import str_prominent


class User(Base, TimestampMixin):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    email = Column(String(255), unique=True, index=True, nullable=False)
    username = Column(String(255), unique=True, index=True, nullable=False)
    nickname = Column(String(255),default='', unique=True, index=True, nullable=False)
    hashed_password = Column(String(255), nullable=False)
    is_active = Column(Boolean, default=True)
    is_superuser = Column(Boolean, default=False)

    # Relationships
    knowledge_bases = relationship("KnowledgeBase", back_populates="user")
    chats = relationship("Chat", back_populates="user")
    api_keys = relationship("APIKey", back_populates="user", cascade="all, delete-orphan")
    ai_api = relationship("AIApiKey", back_populates="user", cascade="all, delete-orphan")

    # 查询的数据转字典

    def to_dict_use_result(self) -> dict:
        model_dict =super().to_dict_use_result()
        del model_dict['hashed_password']
        return model_dict

    # def json_encoders(self):
    #     print(str_prominent('json_encoders'))
    #     return self.to_dict_use_result()